package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.annotation.Arithmetic;
import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.manager.EventConvert;
import com.navinfo.platform.trip.common.arithmetic.var.VarSpeedOver;
import com.navinfo.platform.trip.common.arithmetic.enums.EventType;
import com.navinfo.platform.trip.common.arithmetic.exception.RangeAssert;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.config.ConfigMap;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;

import java.util.Optional;

/**
 * 东风超速行驶算法<br/>
 * 依赖{@link Mileage}里程油耗算法<br/>
 * 输出：统计、事件<br/>
 * @author web
 */
@Arithmetic(name = "东风超速行驶算法")
public class SpeedOver_DF extends AbstractArithmetic<VarSpeedOver> {
    /**
     * 事件类型
     */
    private static final EventType eventType = EventType.speedOver_DF;
    /**
     * 超速行驶，速度阀值90KM/H
     */
    private Integer speedThreshold = ConfigMap.getInteger("DF_Fast_Speed_Threshold");

    @Override
    public void calculate() {
        logger.debug("进入东风超速行驶算法.");
        VarSpeedOver var = super.getObj();
        CommonData commonData = getCommonData();

        //1.速度有效性判断
        RangeAssert.speedKmhIsInTheRange(commonData.getSpeedKmh());

        //2.上一次和本次的超速行驶状态
        boolean preIsOverSpeed = var.isPreOverSpeed();
        boolean curIsOverSpeed = commonData.getSpeedKmh()>= speedThreshold;
        logger.debug("东风超速行驶算法，超速行驶状态：pre:{},current:{}", preIsOverSpeed, curIsOverSpeed);

        //3.算法计算
        switch (getEventStatus(preIsOverSpeed, curIsOverSpeed)) {
            case START:
                logger.debug("东风超速行驶算法，事件开始.[terminalId:{}]", terminalId);
                startEventData(eventType);
                break;
            case IN:
                logger.debug("东风超速行驶算法，事件中.[terminalId:{}]", terminalId);
                calcEventData(eventType);
                break;
            case END:
                logger.debug("东风超速行驶算法，事件结束.[terminalId:{}]", terminalId);
                calcEventData(eventType);
                endEvent();
                break;
            default:
                logger.debug("东风超速行驶算法，超速行驶状态：上一次和本次都未超速行驶，跳出算法.[terminalId:{}]", terminalId);
        }

        //4.最后一个点，如果有未结束的结束
        if (commonData.isTripLastNode()) {
            endEvent();
        }

        var.setPreOverSpeed(curIsOverSpeed);
        logger.debug("东风超速行驶算法结束.");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarSpeedOver var = super.getObj();
        statisticDataOut.setFastSpeedMileage_DF(ArithmeticUtils.mileageMToM(var.getMileage()));
        statisticDataOut.setFastSpeedFuel_DF(ArithmeticUtils.fuelMlToMl(var.getFuel()));
        statisticDataOut.setFastSpeedDuration_DF(ArithmeticUtils.timeMsToSecond(var.getDuration()));
        statisticDataOut.setFastSpeedNumber_DF(var.getNumber());
    }

    /**
     * 事件结束
     */
    private void endEvent() {
        Optional.ofNullable(drivingData.getEventData(eventType)).ifPresent(eventData -> {
            EventConvert.convertEnd(eventData, drivingData);

            //累加事件数据到统计数据中
            VarSpeedOver var = getObj();
            var.addMileage(Optional.ofNullable(eventData.getExtendField05()).orElse(0.0));
            var.addFuel(Optional.ofNullable(eventData.getExtendField06()).orElse(0.0));
            var.addDuration(Optional.ofNullable(eventData.getDuration()).orElse(0L).intValue());
            var.setNumber(var.getNumber() + 1);

            //放入输出事件列表
            drivingData.addEventDataToList(eventData);

            //删除当前缓存
            drivingData.removeEventData(eventType);
        });
    }

}
